// pages/infoEdit/infoEdit.js
import Config from "../../common/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memList: {},
    icon:'',//头像
    name:'',//昵称
    phone: '',
    phones: '',//手机号
    code: '',//验证码
    codename: '获取验证码',
    codeState: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: Config.url + '/api/integralLevel/member_centre_upgrade',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      success(res) {
        console.log(res)
        that.setData({
          memList: res.data.data.responseMap,
          name: res.data.data.responseMap.nickName,
          icon: res.data.data.responseMap.icon
        })
        console.log(that.data.memList)
      }
    })
    // 判断用户是否绑定手机号
    wx.request({
      url: Config.url + '/api/user/getMobile',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      data: {},
      success(res) {
        console.log(res)
        if (res.data.data.mobile == null) {
          that.setData({
            phone: "去绑定",
          })

        }else{
          that.setData({
            phone: res.data.data.mobile,

          })
        }
      }
    })
    
  },
  uploadImg: function (e) {
    var that = this
    wx.chooseImage({
      success: function (res) {
        console.log(res)
        that.setData({
          icon: res.tempFilePaths[0]

        })
        var tempFilePaths = res.tempFilePaths  //图片
      
      }
    })
  },
  // 保存修改
  editBtn: function (e) {
    var that = this
    wx.request({
      url: Config.url + '/api/user/updateData',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      data: { mobile: that.data.phone, nickName: that.data.name, icon: that.data.icon },
      success(res) {
        console.log(res)
        if (res.data.code == 100){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          })
          wx.switchTab({
            url: '../my/my',   
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
       
      }
    })
  },
  editBtn1:function(){
    wx.switchTab({
      url: '../my/my',
    })
  },
  // 绑定手机号
  editPhone: function (e) {
    this.setData({
      codeState: false
    })
  },

  //获取input输入框的值
  getPhoneValue: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getnameValue: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getCodeValue: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取验证码
  getCode: function () {
    var a = this.data.phone;
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: Config.url + '/api/user/SMS_phoneValidate',
        method: 'get',
        header: {
          'content-type': 'application/json',
          'accessToken': wx.getStorageSync('sessionId')
        },
        data: { teleNum: that.data.phone },
        success(res) {
          console.log(res)
          that.setData({
            iscode: res.data.data,
            disabled: true
          })
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              that.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
      })

    }


  },
  //获取验证码按钮
  getVerificationCode() {
    this.getCode();
    var that = this

  },
  //提交表单信息
  save: function () {
    var that = this
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.code == "") {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: Config.url + '/api/user/SMS_verification',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'accessToken': wx.getStorageSync('sessionId')
        },
        data: { mobile: that.data.phone, code: that.data.code },
        success(res) {
          console.log(res.data)
          if (res.data.code == 312) {
            wx.showToast({
              title: res.data.data.msg,
              icon: 'none',
              duration: 1000
            })
          }
          if (res.data.code == 100) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
            var mobile = res.data.data.mobile
            wx.request({
              url: Config.url + '/api/user/updateData',
              method: 'POST',
              header: {
                'content-type': 'application/json',
                'accessToken': wx.getStorageSync('sessionId')
              },
              data: { mobile: mobile },
              success(res) {
                console.log(res)
                that.setData({
                  codeState: true
                })
              }
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})